// miniprogram/pages/bind/bind.js
const app = getApp()
const {request} = require('../../util/request.js')
Page({
    /**
     * 页面的初始数据
     */
    data: {
        couldTap: true,
        seconds: 1,
        phone: null,
        code: null,
        unionid: app.globalData.unionid,
        openid: app.globalData.openid,
        cutDown: null
    },
    phoneChange(e) {
        this.setData({
            phone: e.detail.value
        })
    },
    codeChange(e) {
        this.setData({
            code: e.detail.value
        })
    },
    getCode() {
        if (this.data.couldTap) {
            if (!this.data.phone) {
                wx.showToast({
                    title: '请输入手机号',
                    icon: 'none',
                    duration: 2000
                })
                return false
            }
            if (!(/^1[3456789]\d{9}$/.test(this.data.phone)) || this.data.phone.length != 11) {
                wx.showToast({
                    title: '请输入11位合法手机号',
                    icon: 'none',
                    duration: 2000
                })
                return false
            }
            this.setData({
                couldTap: false
            })
            let header = {
                "content-type": "application/json;charset=UTF-8",
                'Accept': 'application/json',
            }
            var self = this
            wx.showLoading({
                mask: true
            })
            wx.request({
                //项目的真正接口，通过字符串拼接方式实现
                url: `https://bukeapi.gaodunwangxiao.com/v2/util/sms/fetch`,
                header: header,
                data: {mobile: this.data.phone,auth:"56c5173e5889f983b097c669741d50a4",type:2},
                method: 'POST',
                success: function (v) {
                    //参数值为res.data,直接将返回的数据传入
                    wx.hideLoading()
                    let i = 60
                    let timer = setInterval(() => {
                        if (i <= 0) {
                            clearInterval(timer)
                            this.setData({
                                couldTap: true,
                                seconds: 60
                            })
                        }
                        i--;
                        this.setData({
                            seconds: i
                        })
                    }, 1000)
                    this.setData({
                        cutDown: timer
                    })
                },
                fail: function () {
                    wx.showToast({
                        icon: 'none',
                        title: '网络错误，请重试',
                    })
                    this.setData({
                        couldTap: true
                    })
                    wx.hideLoading()
                },
            })
        }
    },
    skipBind() {
        let options = {
            unionid: this.data.unionid,
            openid: this.data.openid,
            name: app.globalData.userInfo.nickName,
            gender: app.globalData.userInfo.gender,
            iconurl: app.globalData.userInfo.avatarUrl,
        }
        this.bindRequest(options)
    },
    bind() {
        let options = {
            unionid: this.data.unionid,
            openid: this.data.openid,
            mobile: this.data.phone,
            code: this.data.code,
            name: app.globalData.userInfo.nickName,
            gender: app.globalData.userInfo.gender,
            iconurl: app.globalData.userInfo.avatarUrl,
        }
        if (!options.phone) {
            wx.showToast({
                title: '请输入手机号',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (!(/^1[3456789]\d{9}$/.test(options.mobile)) || options.mobile.length != 11) {
            wx.showToast({
                title: '请输入11位合法手机号',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        if (!options.code) {
            wx.showToast({
                title: '请输入验证码',
                icon: 'none',
                duration: 2000
            })
            return false
        }
        this.bindRequest(options)
    },
    bindRequest(options) {
        let url = '/mike/auth/register'
        if (app.globalData.inviterId) {
            // 推荐注册
            options.inviter_id = app.globalData.inviterId
            options.product_id = app.globalData.productId
            url = '/mike/auth/invite_register'
        }
        request(url, 'POST', options,
            (res) => {
                if (res.error === 0) {
                    wx.showToast({
                        title: '绑定成功',
                        icon: 'success',
                        duration: 2000
                    })
                    app.globalData.userId = res.data.user.user_id
                    wx.setStorage({
                        key: 'token',
                        data: res.data.auth.token_type + ' ' + res.data.auth.access_token,
                    })
                    wx.setStorage({
                        key: 'user_id',
                        data: res.data.user.user_id,
                    })
                    setTimeout(() => {
                        wx.navigateBack()
                    },1000)

                } else {
                    wx.showToast({
                        title: res.message,
                        icon: 'none',
                        duration: 2000
                    })
                }
            },
            (error) => {
                wx.showToast({
                    title: '失败',
                    duration: 2000
                })
            })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        if (!this.data.couldTap) {
            clearInterval(this.data.cutDown)
        }
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function (res) {
        return {
            title: '每天半小时，轻松拿本科',
            path: '/pages/index/index',//实验发现，此处的路径应该写全，写到具体的wxml页面。如path:'/page/user/user?id=123'
            success: function(res) {
                // 转发成功
            },
            fail: function(res) {
                // 转发失败
            }
        }
    }
})
